package com.localShare.activity;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.widget.MediaController;
import android.widget.VideoView;

import com.localShare.Constant;
import com.localShare.R;
import com.util.SharedPreferencesUtil;
import com.util.WindowManagerUtil;

/**
 * 视频加载显示
 */
public class VideoActivity extends BaseActivity{
    //屏幕管理工具类
    private final WindowManagerUtil windowManagerUtil =new WindowManagerUtil();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //锁屏时也可显示
        windowManagerUtil.showWhenLocked(this);
        //布局
        setContentView(R.layout.activity_video);
        //全屏
        windowManagerUtil.fullScreen(this);
        //视频播放控件
        VideoView videoView=(VideoView)findViewById(R.id.videoView);
        //视频控制控件
        MediaController controller = new MediaController(this);
        //视频控制控件位置
        controller.setAnchorView(videoView);
        //视频显示控件和控制控件相互绑定
        controller.setMediaPlayer(videoView);
        videoView.setMediaController(controller);
        //首选项工具类
        SharedPreferencesUtil sharedPreferencesUtil=new SharedPreferencesUtil();
        //获取意图，及意图的flag，根据flag不同加载数据
        Intent intent=getIntent();
        int flag=intent.getFlags();
        switch(flag){
            //从Intent中加载数据，同时保存
            case Constant.COM_FROM_CURRENT:
                //获取intent中的数据
                Bundle extras=intent.getExtras();
                if(null!=extras){
                    //获取uri
                    Uri uri=extras.getParcelable(Intent.EXTRA_STREAM);
                    //非空判断
                    if(null!=uri){
                        //将uri设给视频显示控件
                        videoView.setVideoURI(uri);
                        //开始播放
                        videoView.start();
                        //数据存储
                        sharedPreferencesUtil.saveInt(this,Constant.PRIVATE_SHARED_PREFERENCE,Constant.LAST_LOAD_FLAG,Constant.LAST_LOAD_VIDEO);
                        sharedPreferencesUtil.saveString(this,Constant.PRIVATE_SHARED_PREFERENCE,Constant.VIDEO_URI,uri.toString());
                    }
                }
                break;
            //从历史存储中取数据
            case Constant.COM_FROM_HISTORY:
                //取数据
                String uriString=sharedPreferencesUtil.getString(this,Constant.PRIVATE_SHARED_PREFERENCE,Constant.VIDEO_URI,Constant.VIDEO_URI_INIT);
                //非空判断
                if(!Constant.VIDEO_URI_INIT.equals(uriString)){
                    //数据解析
                    Uri uri=Uri.parse(uriString);
                    //设置给视频显示控件
                    videoView.setVideoURI(uri);
                    //开始播放
                    videoView.start();
                }
                break;
        }
    }
}
